package com.qqt.csr.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.qqt.csr.common.utils.JsonUtil;
import com.qqt.csr.order.entity.WorkOrderEvent;
import com.qqt.csr.order.enums.WorkOrderStatusEnum;
import com.qqt.csr.order.mapper.WorkOrderEventMapper;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class WorkOrderEventService extends ServiceImpl<WorkOrderEventMapper, WorkOrderEvent> {

    /**
     * 查询工单最后{@code WorkOrderStatusEnum}事件
     *
     * @param orderIds {@link List<Long>} 工单id集
     * @param status   {@link WorkOrderStatusEnum} 工单状态
     * @return {@link List<WorkOrderEvent>} 工单事件集
     */
    public List<WorkOrderEvent> listLastEvent(List<Long> orderIds, WorkOrderStatusEnum status) {
        if (orderIds == null || orderIds.isEmpty()) {
            return Collections.emptyList();
        }
        return this.baseMapper.listLastEvent(orderIds, status);
    }

    /**
     * 查询工单最后{@code WorkOrderStatusEnum}事件
     *
     * @param orderId {@link Long} 工单id
     * @param status  {@link WorkOrderStatusEnum} 工单状态
     * @return {@link WorkOrderEvent} 工单事件
     */
    public WorkOrderEvent lastEvent(Long orderId, WorkOrderStatusEnum status) {
        LambdaQueryWrapper<WorkOrderEvent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(orderId), WorkOrderEvent::getOrderId, orderId);
        wrapper.eq(Objects.nonNull(status), WorkOrderEvent::getTo, status.getStatus());
        wrapper.orderByDesc(WorkOrderEvent::getCreateTime);
        wrapper.last("limit 1");
        return this.getOne(wrapper);
    }

    /**
     * 保存事件
     *
     * @param context
     */
    public void insert(WorkOrderContext context) {
        WorkOrderEvent workOrderEvent = new WorkOrderEvent()
                .setOrderId(context.getToOrder().getId())
                .setEventType(context.getEvent().getCode())
                .setFrom(context.getFromStatus().getStatus())
                .setTo(context.getToOrder().getStatus())
                .setCreatorId(context.getOperatorId())
                .setExt(Optional.ofNullable(context.getEventExt()).map(JsonUtil::toJson).orElse(null));
        this.save(workOrderEvent);
    }

    /**
     * 根据工单id查询事件列表
     *
     * @param orderIdList
     * @return
     */
    public Map<Long, List<WorkOrderEvent>> getMapBy(List<Long> orderIdList) {
        if (CollectionUtils.isEmpty(orderIdList)) {
            return Maps.newHashMap();
        }
        return this.lambdaQuery().select(WorkOrderEvent::getOrderId, WorkOrderEvent::getFrom,
                        WorkOrderEvent::getTo, WorkOrderEvent::getCreateTime)
                .in(WorkOrderEvent::getOrderId, orderIdList)
                .list()
                .stream().collect(Collectors.groupingBy(WorkOrderEvent::getOrderId));
    }
}
